<?php
    class ProductController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        
        public function init()
        {
            $this->loadTemplates();
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Default_Model_Product();
        }
        
        public function categoryAction()
        {
            $id = (int) $this->_arrParam['id'];
            $category = new Default_Model_Category();
            $this->view->categoryName = $category->getName($id);
            $this->view->headTitle('Nội thất Bảo Toàn - '.$this->view->categoryName);
            $this->view->keywords = 'Bảo Toàn, nội thất, Bao Toan, noi that, sản phẩm, san pham, '.$this->view->categoryName;
            $this->view->description = 'Nội thất Bảo Toàn - '.$this->view->categoryName;
            $result = $category->getChild($id);
            if(count($result) > 0) {
                $this->view->category = $result;
                $this->render('index');
            }
            else {
                $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->getItemByCategory($id));
                $paginator = new Zend_Paginator($adapter);
                $paginator->setItemCountPerPage(30);
                $paginator->setPageRange(5);
                $currentPage = $this->_request->getParam('page', 1);
                $paginator->setCurrentPageNumber($currentPage);
                $this->view->data = $paginator;
                $this->view->category = $category->getItemForPaginator($id);
                $this->render('category');
            }
        }
        
        public function detailAction()
        {
            $id = (int) $this->_arrParam['id'];
            $this->view->data = $this->_model->getItem($id);
            $this->view->headTitle('Nội thất Bảo Toàn - '.$this->view->data['name']);
            $this->view->keywords = 'Bảo Toàn, nội thất, Bao Toan, noi that, sản phẩm, san pham, '.$this->view->data['name'];
            $this->view->description = 'Nội thất Bảo Toàn - '.$this->view->data['name'];
            $this->view->dataConnect = $this->_model->getItemConnect($this->view->data['category_id'], $id);
        }
        
        public function searchAction()
        {
            $this->view->headTitle('Nội thất Bảo Toàn - Tìm kiếm sản phẩm');
            $strFlex = new Zendvn_Filter_RemoveCircumflex();
            $name = trim(strtolower($strFlex->filter2(strip_tags($this->_arrParam['name']))));
            $start_price = trim(strip_tags($this->_arrParam['start_price']));
            $end_price = trim(strip_tags($this->_arrParam['end_price']));
            $category_id = trim(strip_tags($this->_arrParam['category']));
            $provider_id = trim(strip_tags($this->_arrParam['provider']));
            
            $this->view->keyword = array(
                'name' => $this->_arrParam['name'],
                'category' => $this->_arrParam['category'],
                'provider' => $this->_arrParam['provider'],
                'start_price' => $this->_arrParam['start_price'],
                'end_price' => $this->_arrParam['end_price'],
            );
            $data = array(
	            'name' => (string) '%'.str_replace(' ','%',$name).'%',
                'category_id' => $category_id,
                'provider_id' => $provider_id,
                'start_price' => $start_price,
                'end_price' => $end_price,
		    );
		    $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->searchItem($data));
		    $paginator = new Zend_Paginator($adapter);
		    $paginator->setItemCountPerPage(1);
		    $paginator->setPageRange(5);
		    $currentPage = $this->_request->getParam('page', 1);
		    $paginator->setCurrentPageNumber($currentPage);
		    $this->view->data = $paginator;
        }
    }
